package com.june.junemplayer.mapper;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;

import com.june.junemplayer.pojo.Song;
import com.june.junemplayer.pojo.SongList;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class SongMapper {

    public void insert(Context context, Song song) {
        Log.d("JuneSQLite", "----Song----insert() start:song = " + song + "----");
        SongSQLiteOpenHelper songSQLiteOpenHelper = new SongSQLiteOpenHelper(context, "MusicPlayerDatabase.db", null, SongSQLiteOpenHelper.getVersion());
        SQLiteDatabase database = songSQLiteOpenHelper.getWritableDatabase();
        ContentValues value = new ContentValues();
        value.put("title", song.getTitle());
        value.put("path", song.getPath());
        value.put("artist", song.getArtist());
        value.put("duration", song.getDuration());
        value.put("size", song.getSize());
        value.put("lyric", song.getLyric());
        value.put("extra", song.getExtra());
        database.insert("Song", null, value);
        Log.d("JuneSQLite", "----Song----insert() end:value = " + value + "----");
    }

    public int delete(Context context, String path) {
        Log.d("JuneSQLite", "----Song----delete() start:path = " + path + "----");
        SongSQLiteOpenHelper songSQLiteOpenHelper = new SongSQLiteOpenHelper(context, "MusicPlayerDatabase.db", null, SongSQLiteOpenHelper.getVersion());
        SQLiteDatabase database = songSQLiteOpenHelper.getWritableDatabase();
        String[] paths = new String[]{path};
        Log.d("JuneSQLite", "----Song----delete() end----");
        return database.delete("Song", "path = ? ", paths);
    }

    public int deleteAll(Context context) {
        Log.d("JuneSQLite", "----Song----deleteAll() start----");
        SongSQLiteOpenHelper songSQLiteOpenHelper = new SongSQLiteOpenHelper(context, "MusicPlayerDatabase.db", null, SongSQLiteOpenHelper.getVersion());
        SQLiteDatabase database = songSQLiteOpenHelper.getWritableDatabase();
        Log.d("JuneSQLite", "----Song----deleteAll() end----");
        return database.delete("Song", null,null);
    }

    public void update(Context context, Song song) {
        Log.d("JuneSQLite", "----Song----update() start:song = " + song + "----");
        if (delete(context, song.getPath()) == -1) {
            Log.e("JuneSQLiteFalse", "----Song----delete() false----");
            Log.e("JuneSQLiteFalse", "-----Song-----update() false-----");
            return;
        }
        insert(context, song);
        Log.d("JuneSQLite", "----Song----update() end----");
    }

    @SuppressLint("Range")
    public ArrayList<Song> retrieveAllSongs(Context context) {
        Log.d("JuneSQLite", "----Song----retrieveAllSongs() start----");
        SongSQLiteOpenHelper songSQLiteOpenHelper = new SongSQLiteOpenHelper(context,
                "MusicPlayerDatabase.db", null, SongSQLiteOpenHelper.getVersion());
        SQLiteDatabase database = songSQLiteOpenHelper.getWritableDatabase();
        Cursor cursor = database.query("Song", null, null,
                null, null, null, null);
        ArrayList<Song> songs = new ArrayList<>();
        int count = 0;
        if (cursor.moveToFirst()) {
            do {
                Song song = new Song();
                song.setTitle(cursor.getString(cursor.getColumnIndex("title")));
                song.setPath(cursor.getString(cursor.getColumnIndex("path")));
                song.setArtist(cursor.getString(cursor.getColumnIndex("artist")));
                song.setDuration(cursor.getInt(cursor.getColumnIndex("duration")));
                song.setSize(cursor.getLong(cursor.getColumnIndex("size")));
                song.setLyric(cursor.getLong(cursor.getColumnIndex("lyric")));
                song.setExtra(cursor.getString(cursor.getColumnIndex("extra")));
                Log.d("JuneSQLiteInfo", count + "----" + song);
                count++;
                songs.add(song);
            } while (cursor.moveToNext());
        }
        cursor.close();
        Log.d("JuneSQLite", "----Song----retrieveAllSongs() end:count = "+count+"----");
        return songs;
    }

    public static class SongSQLiteOpenHelper extends SQLiteOpenHelper {

        private static int mVersion = 1;
        String createSong = "create table Song(path text primary key,title text,artist text,duration integer,size integer,lyric integer,extra text)";
        String createSongList = "create table SongList(title text primary key,owner integer,path text,description text,cover text,number integer,frequency integer,visible integer,extra text)";
        String createSongListContent = "create table SongListContent(title integer,path integer,number integer,primary key(title,path))";


        public SongSQLiteOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
            mVersion = (version == 0 ? 1 : version);
        }

        @Override
        public void onCreate(SQLiteDatabase sqLiteDatabase) {
            sqLiteDatabase.execSQL(createSong);
            sqLiteDatabase.execSQL(createSongList);
            sqLiteDatabase.execSQL(createSongListContent);
            Log.d("JuneSQLite", "创建成功----Song----版本" + sqLiteDatabase.getVersion());
        }

        @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
//        sqLiteDatabase.execSQL(update);
            Log.d("JuneSQLite", "更新成功----Song----版本" + oldVersion + "  ->  " + newVersion);
            mVersion = newVersion;
        }

        public static int getVersion() {
            return mVersion;
        }
    }
}